package com.jaya.budan.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;

import com.jaya.budan.crop.Crop;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.google.gson.Gson;
import com.jaya.budan.R;
import com.jaya.budan.base.BaseActivity;
import com.jaya.budan.connect.RequestConnect;
import com.jaya.budan.utils.ImageUtil;
import com.jaya.budan.utils.ToolUtils;
import com.jaya.budan.utils.VolleyUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;

public class Crop_Picture_Activity extends BaseActivity implements View.OnClickListener {

    private RelativeLayout back;
    private TextView tv_crop_picture;
    private ImageView iv_crop_picture;
    private String path;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_crop_picture);
        Intent intent = getIntent();
        path = intent.getStringExtra("path");
        back = findViewById(R.id.back);
        tv_crop_picture = findViewById(R.id.tv_crop_picture);
        iv_crop_picture = findViewById(R.id.iv_crop_picture);
        back.setOnClickListener(this);
        tv_crop_picture.setOnClickListener(this);
        Bitmap bitmap = ImageUtil.getResizedBitmap(path, null, this, null, 100, false);
        iv_crop_picture.setImageBitmap(bitmap);
//        Crop.pickImage(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tv_crop_picture:
                SharedPreferences preferences = getSharedPreferences("photo_path", Context.MODE_PRIVATE);
                SharedPreferences.Editor editor = preferences.edit();
                editor.putString("path", path);
                editor.apply();
                finish();
                break;
            case R.id.back:
                finish();
                break;
            default:
                break;
        }
    }

//    @Override
//    public boolean onCreateOptionsMenu(Menu menu) {
//        getMenuInflater().inflate(R.menu.activity_main, menu);
//        return super.onCreateOptionsMenu(menu);
//    }
//
//    @Override
//    public boolean onOptionsItemSelected(MenuItem item) {
//        if (item.getItemId() == R.id.action_select) {
//            iv_crop_picture.setImageDrawable(null);
//            Crop.pickImage(this);
//            return true;
//        }
//        return super.onOptionsItemSelected(item);
//    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent result) {
        super.onActivityResult(requestCode, resultCode, result);
        if (requestCode == Crop.REQUEST_PICK && resultCode == RESULT_OK) {
            beginCrop(result.getData());
        } else if (requestCode == Crop.REQUEST_CROP) {
            handleCrop(resultCode, result);
        }
    }

    private void beginCrop(Uri source) {
        Uri destination = Uri.fromFile(new File(getCacheDir(), "cropped"));
        Crop.of(source, destination).asSquare().start(this);
    }

    private void handleCrop(int resultCode, Intent result) {
        if (resultCode == RESULT_OK) {
            iv_crop_picture.setImageURI(Crop.getOutput(result));

        } else if (resultCode == Crop.RESULT_ERROR) {
            ToolUtils.showToast(Crop_Picture_Activity.this, Crop.getError(result).getMessage());
        }
    }

    /**
     * 网络请求
     * @param object
     */
    private void postData(JSONObject object) {
        JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, RequestConnect.register_Url, object, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                Log.d("register", jsonObject.toString());
                try {
                    int code = jsonObject.optInt("code");
                    int status = jsonObject.optInt("status");
                    String message = jsonObject.getString("message");
                    if (code == 0) {
                        Gson gson = new Gson();
                        JSONObject info = jsonObject.getJSONObject("info");
                        String access_token = info.getString("access_token");
//                        Intent intent = new Intent(MainActivity.this, Register3_Activity.class);//已注册，自动跳转至首页页面
//                        startActivity(intent);
//                        finish();
                    } else {
                        ToolUtils.showToast(Crop_Picture_Activity.this, message);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                ToolUtils.showToast(Crop_Picture_Activity.this, "网络请求失败，请稍后重试~");
            }
        });
        request.setTag(this);
        VolleyUtil.getQueue(this).add(request);
    }
}
